from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from core.pagination import DependsPagination, PageData

from modules.project.services.project_service import ProjectService
from modules.project.schemas.project_schema import ProjectCreate, ProjectUpdate
from modules.project.params.project_params import ProjectQuery
from modules.project.vos.project_vo import ProjectResponse


router = APIRouter()


@router.post(
    "/list",
    response_model=ApiResponse[PageData[ProjectResponse]],
    summary="获取项目表列表",
    dependencies=[DependsPagination],
)
async def list_projects(query_params: ProjectQuery, db: AsyncSession = Depends(get_db)):
    result = await ProjectService.get_list_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.get("/detail/{pk}", response_model=ApiResponse[ProjectResponse], summary="获取项目表详情")
async def get_project(pk: int, db: AsyncSession = Depends(get_db)):
    result = await ProjectService.get_by_id(db, pk)
    return api_success(data=result, msg="获取成功")


@router.post("/create", response_model=ApiResponse[ProjectResponse], status_code=status.HTTP_201_CREATED, summary="创建项目表")
async def create_project(body: ProjectCreate, db: AsyncSession = Depends(get_db)):
    result = await ProjectService.create(db, body)
    return api_success(data=result, msg="创建成功")


@router.put("/update/{pk}", response_model=ApiResponse[ProjectResponse], summary="更新项目表")
async def update_project(pk: int, body: ProjectUpdate, db: AsyncSession = Depends(get_db)):
    result = await ProjectService.update(db, pk, body)
    return api_success(data=result, msg="更新成功")


@router.delete("/delete/{pk}", response_model=ApiResponse[dict], summary="删除项目表")
async def delete_project(pk: int, db: AsyncSession = Depends(get_db)):
    ok = await ProjectService.delete(db, pk)
    return api_success({"success": ok}, msg="删除成功" if ok else "未删除")
